import { prisma } from "@/lib/prisma"
import { getServerSession } from "next-auth"
import { NextResponse } from "next/server"
import { authOptions } from "@/lib/auth"

export async function POST(
  req: Request,
  context: { params: { id: string } }
) {
  const { id } = await Promise.resolve(context.params)
  
  try {
    const session = await getServerSession(authOptions)
    if (!session?.user?.isAdmin) {
      return NextResponse.json({ error: "未授权访问" }, { status: 401 })
    }

    const { csv } = await req.json()
    const rows = csv.split("\n").filter(Boolean)

    const events = rows.map(row => {
      const [title, description, date] = row.split(",").map(s => s.trim())
      return { title, description, date: new Date(date), libraryId: id }
    })

    await prisma.event.createMany({
      data: events
    })

    return NextResponse.json({ success: true })

  } catch (error) {
    return NextResponse.json(
      { error: "导入事件失败" },
      { status: 500 }
    )
  }
} 